<?php

namespace app\api\controller\wanlshop;

use addons\wanlshop\library\Decrypt\weixin\wxBizDataCrypt;
use addons\wanlshop\library\WanlChat\WanlChat;
use addons\wanlshop\library\WeixinSdk\Security;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\api\controller\wanlshop\Common;

use app\common\model\AddressLocation;
use app\common\model\GradeOrder;
use app\common\model\User as UserModel;
use fast\Arr;
use fast\Random;
use fast\Http;
use think\Db;
use think\Validate;

/**
 *积分/消费券
 */
class PointsCard extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
    /**
     * 红积分(补贴列表)
     */
    public function red_points_list()
    {
        $start_time = $this->request->post('start_time');
        $end_time = $this->request->post('end_time');
		$where['user_id']=$this->auth->id;
		$where['get_type']=1;
		if(!empty($start_time)&&!empty($end_time)){
           $where['createtime']=['between',[strtotime($start_time),strtotime($end_time)]];
		}

		$this->request->filter(['strip_tags']);
		$list = model('app\common\model\RedPointsLog')
		    ->field('order_no,createtime,points')
			->where($where)
			->order('updatetime desc')
			->paginate();
		$this->success('返回成功', $list);
	}

	 /**
     * 红积分（兑换记录）
     */
    public function red_points_use()
    {
		$start_time = $this->request->post('start_time');
        $end_time = $this->request->post('end_time');
		$where['a.user_id']=$this->auth->id;
		if(!empty($start_time)&&!empty($end_time)){
           $where['a.createtime']=['between',[strtotime($start_time),strtotime($end_time)]];
		}

		$this->request->filter(['strip_tags']);
		$list = model('app\common\model\ConsumCard')
		        ->alias('a')
                ->join('red_points_log b','a.order_id=b.order_id and a.user_id=b.user_id')
				->field('b.order_no,a.createtime,b.points,a.money')
				->where($where)
				->order('a.updatetime desc')
				->paginate();
		$this->success('返回成功', $list);
	}

	/**
     * 消费券（补贴明细/使用记录）
     */
    public function consum_card_list()
    {
		$start_time = $this->request->post('start_time');
        $end_time = $this->request->post('end_time');
        $type = $this->request->post('type');
		if($type){
			if($type==1){//1补贴明细2使用记录
				$where['money']=['>',0];
			}elseif($type==2){
				$where['money']=['<',0];
			}
		}
		$where['user_id']=$this->auth->id;
		// $where['user_id']=9;
		if(!empty($start_time)&&!empty($end_time)){
           $where['createtime']=['between',[strtotime($start_time),strtotime($end_time)]];
		}

        $get_type_arr=[1=>'红积分兑换获得',2=>'绿积分兑换获得',3=>'消费使用',4=>'全国运营中心获得',5=>'红积分退款',6=>'全国运营中心退款'];

		$this->request->filter(['strip_tags']);
		$list = model('app\common\model\ConsumCard')
				->field('order_no,createtime,money,get_type')
				->where($where)
				->order('updatetime desc')
				->paginate();
		foreach($list as $v){
			$v['get_type_str']=$get_type_arr[$v['get_type']]??'其他方式';
		}		

		$this->success('返回成功', $list);
	}
}